package com.hyl.icon.controller;

import com.hyl.icon.common.BaseResponse;
import com.hyl.icon.common.ErrorCode;
import com.hyl.icon.common.ResultUtils;
import com.hyl.icon.model.entity.User;
import com.hyl.icon.service.IconService;
import com.hyl.icon.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.awt.image.BufferedImage;


@RestController
@RequestMapping("/icon")
@Slf4j
public class IconController {

    @Resource
    private IconService iconService;

    @Resource
    private UserService userService;

    @PostMapping("/generate")
    public BaseResponse<String> generateBrandLogo(
            @RequestParam("background") MultipartFile backgroundFile,
            @RequestParam("logo") MultipartFile logoFile,
            @RequestParam("brandName") String brandName, @RequestParam("phone")String phone, @RequestParam("address")String address, HttpServletRequest request) {
        //1.必须登录
        User loginUser = userService.getLoginUser(request);
        if (loginUser == null){
            return ResultUtils.error(ErrorCode.NOT_LOGIN_ERROR,"请登录后使用");
        }
        // 调用生成品牌Logo的服务，之前的方法
//        BufferedImage bufferedImage = iconService.generateBrandImage(backgroundFile, logoFile, brandName);
        BufferedImage bufferedImage = iconService.getTemplateResult(backgroundFile,logoFile,brandName,phone,address);
        // 转化为base64字符串返回
        String image = iconService.imageToBase64(bufferedImage, "png");

//        String outputPath = "D:\\workspace\\icon\\test\\images\\output_logo.png";    // 输出图片路径
//        try {
//            ImageIO.write(bufferedImage, "PNG", new File(outputPath));
//        } catch (IOException e) {
//            throw new RuntimeException(e);
//        }
        return ResultUtils.success(image);
    }
}
